<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Front;

class Contact extends Front {

	protected $beforeActionList = array(
		'setModel' => array('except' => 'category'),
	);
	
	public function detail($id = '', $name = '')
	{
	    header("Content-type: text/html; charset=utf-8");
	    // 获取内容
	    $info = $this->model->find($id);
		if (empty($info)) {
			return $this->error("无此内容！");
		}
		// 获取父栏目
		$pCate = getParentCategory($info['category_id']);
		// 获取当前分类
		$cate = $this->getCategory($info['category_id']);
		// 联系方式
		$contact = M('contact')->where(['id'=>1])->find();
		// 提交表单
		$m = $this->request->param('model');
		$form = db('Form')->where('name', $m)->find();
		$attrModel = M('attr', 'form');
		$attrs = $attrModel->where(['form_id'=>$form['id'], 'is_show'=>1])->select();
		$data = array(
		    'pCate'=> $pCate,
			'info' => $info,
		    'cate' => $cate,
		    'contact' => $contact,
		    'attrs' => $attrs,
		    'formId' => $form['id'],
		);
		if (isset($info['template_detail']) && $info['template_detail']) 
		{
			$teamplate = 'content/' . $this->modelInfo['name'] . '/' . $info['template_detail'];
		}
		else
		{
			$teamplate = 'content/' . $this->modelInfo['name'] . '/detail';
		}
		$this->assign($data);
		$title       = isset($info['title']) ? $info['title'] : '';
		$tags        = isset($info['tags']) ? $info['tags'] : '';
		$description = isset($info['description']) ? $info['description'] : '';
		$this->setSeo($title, $tags, $description);
		return $this->fetch($teamplate);
	}
	
	/**
	 * 表单数据提交
	 */
	public function add(\think\Request $request){
	    if ($request->isPost()) {
	        $data = $request->param();
	        $model_name = $this->request->param('model');
	        $result = M($model_name, 'form')->save($data);
	        if (false !== $result) {
	            return $this->success('提交成功！');
	        }else{
	            return $this->error('提交失败！');
	        }
	    }else{
	        return $this->error('非法操作！');
	    }
	}
	
	protected function getCategory($id) {
		$data = db('Category')->find($id);
		return $data;
	}

	protected function setModel() {
		$model_name = $this->request->param('model');
		$model_id   = $this->request->param('model_id');
		$row        = db('Model')->select();
		foreach ($row as $key => $value) {
			$name_list[$value['name']] = $value;
			$id_list[$value['id']]     = $value;
		}

		if (empty($name_list[$model_name]) && empty($id_list[$model_id])) {
			return $this->error("无此模型！");
		} else {
			$this->modelInfo = $model_name ? $name_list[$model_name] : $id_list[$model_id];
			$this->model = M($this->modelInfo['name']);

			$this->assign('model_id', $this->modelInfo['id']);
			$this->assign('model_list', $name_list);
		}
	}
}